/* $Id: NonClientPersonDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.NonClientPersonDAO;
import ae.pcfc.etks.online.common.domain.ClientPersonReq;

@Repository
public class NonClientPersonDAOImpl extends BaseDAO implements	NonClientPersonDAO 
{

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.NonClientPersonDAO#add(ae.pcfc.etks.online.common.domain.ClientPersonReq)
	 * 
	 */
	public void add(ClientPersonReq clientPersonReq) 
	{
		getHibernateTemplate().getSessionFactory().getCurrentSession().save(clientPersonReq);		
	}

	/**
	 * getNonClientPersonByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.NonClientPersonDAO#getNonClientPersonByApplicationId(java.lang.String)
	 * 
	 */
	public ClientPersonReq getNonClientPersonByApplicationId(String applicationId) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ClientPersonReq.class);
		criteria.add(Restrictions.eq("applicationId", applicationId));
		@SuppressWarnings("unchecked")
		List<ClientPersonReq> list = criteria.list();
		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * findByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.dao.NonClientPersonDAO#findByPrimaryKey(java.lang.String)
	 * 
	 */
	public ClientPersonReq findByPrimaryKey(String id) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ClientPersonReq.class);
		criteria.add(Restrictions.eq("applicationId", new Long(id)));
		@SuppressWarnings("unchecked")
		List<ClientPersonReq> list = criteria.list();
		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * findByPropertyName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.NonClientPersonDAO#findByPropertyName(java.lang.String, java.lang.String)
	 * 
	 */
	public ClientPersonReq findByPropertyName(String propertyName, String propertyValue) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ClientPersonReq.class);
		criteria.add(Restrictions.eq(propertyName, propertyValue).ignoreCase());
		@SuppressWarnings("unchecked")
		List<ClientPersonReq> list = criteria.list();
		if (list.size() > 0)
			return list.get(0);
		else
			return null;

	}

	/**
	 * validateByPropertyName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.NonClientPersonDAO#validateByPropertyName(java.lang.String, java.lang.String)
	 * 
	 */
	public boolean validateByPropertyName(String strPropertyName,
			String strPropertyValue) {
		org.hibernate.Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(" from ClientPersonReq clientreq , AppMetaData appMeta where clientreq.applicationId =  appMeta.applicationId and appMeta.wfStatus != :status and lower(clientreq."+strPropertyName+") = lower(:propvalue)");
		query.setParameter("status", ConstantsCommon.REJECT);
		query.setParameter("propvalue", strPropertyValue);
		query.list();		
		List<ClientPersonReq> list = query.list();
		if (list != null && list.size() > 0)
			return false;
		else
			return true;
	}

}
